<template>
  <div>
    <div class="search">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="客户姓名:">
          <el-input v-model="customerData.name" placeholder="请输入客户姓名" clearable />
        </el-form-item>
        <el-form-item label="电话:">
          <el-input v-model="customerData.phone" placeholder="请输入电话" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getdata">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="dialogVisible = true">添加</el-button>
        </el-form-item>
      </el-form>

    </div>
    <div>
      <el-table :data="tableData" border>
        <el-table-column prop="id" label="序号" />
        <el-table-column prop="name" label="客户名称" />
        <el-table-column prop="address" label="客户地址" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column prop="phone" label="电话" />
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button size="small" type="danger" @click="remove(scope.row.id)">删除</el-button>
            <el-button size="small" type="primary" @click="update(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        :page-size="limit"
        :total="total"
        style="padding:30px 0;text-align:center;"
        layout="total,prev,pager,next,jumper"
        @current-change="getPagedata"
      />
    </div>

    <el-dialog
      title="添加客户信息"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >

      <el-form ref="customerInfo" :model="customerInfo" label-suffix=":" label-position="left" :rules="rules">
        <el-form-item label="客户名称" prop="name">
          <el-input v-model="customerInfo.name" placeholder="请输入客户名称" style="width: 300px" />
        </el-form-item>
        <el-form-item label="客户地址" prop="address">
          <el-input v-model="customerInfo.address" placeholder="请输入地址" style="width: 300px" />
        </el-form-item>
        <el-form-item label="客户邮箱" prop="email">
          <el-input v-model="customerInfo.email" placeholder="请输入邮箱" style="width: 300px" />
        </el-form-item>
        <el-form-item label="电话号码" prop="phone">
          <el-input v-model="customerInfo.phone" placeholder="请输入电话号码" style="width: 300px" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCustomer('customerInfo')">确认</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="修改客户信息"
      :visible.sync="updatedialogVisible"
      width="30%"
      :before-close="handleClose"
    >

      <el-form ref="customersInfo" :model="customersInfo" label-suffix=":" label-position="left" :rules="ruleses">
        <el-form-item label="客户名称" prop="name">
          <el-input v-model="customersInfo.name" placeholder="请输入客户名称" style="width: 300px" />
        </el-form-item>
        <el-form-item label="客户地址" prop="address">
          <el-input v-model="customersInfo.address" placeholder="请输入地址" style="width: 300px" />
        </el-form-item>
        <el-form-item label="客户邮箱" prop="email">
          <el-input v-model="customersInfo.email" placeholder="请输入邮箱" style="width: 300px" />
        </el-form-item>
        <el-form-item label="电话号码" prop="phone">
          <el-input v-model="customersInfo.phone" placeholder="请输入电话号码" style="width: 300px" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updatedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateCustomer('customersInfo')">确认修改</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import customerApi from '@/api/customer/customer'
export default {
  data() {
    return {
      customerData: {

      },
      tableData: null,
      current: 1, // 当前页
      limit: 6, // 每页显示记录数
      total: 0, // 总记录数
      dialogVisible: false,
      customerInfo: {
        name: '',
        address: '',
        email: '',
        phone: ''
      },
      rules: {
        name:
          [{ required: true, message: '请输入客户名称', trigger: 'blur' },
            { pattern: /^[\u4e00-\u9fa5]{2,4}$/, message: '请输入2到4个汉字', trigger: 'blur' }
          ],
        address:
          [{ required: true, message: '请输入客户地址', trigger: 'blur' }],
        email:
          [{ required: true, message: '请输入客户邮箱', trigger: 'blur' },
            { pattern: /^\S+@qq\.com$/, message: '请输入以@qq.com结尾的邮箱', trigger: 'blur' }
          ],
        phone:
          [{ required: true, message: '请输入客户电话', trigger: 'blur' },
            { pattern: /^\d{11}$/, message: '请输入11位手机号码', trigger: 'blur' }
          ]
      },
      updatedialogVisible: false,
      customersInfo: {
        name: '',
        address: '',
        email: '',
        phone: ''
      },
      ruleses: {
        name:
          [{ required: true, message: '请输入客户名称', trigger: 'blur' }],
        address:
          [{ required: true, message: '请输入客户地址', trigger: 'blur' }],
        email:
          [{ required: true, message: '请输入客户邮箱', trigger: 'blur' }],
        phone:
          [{ required: true, message: '请输入客户电话', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getdata()
  },
  methods: {
    remove(id) {
      this.$confirm('此操作将永久删除,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => { // 确定执行then方法
        // 调用接口
        customerApi.deleteCustomer(id)
          .then(response => {
            // 提示
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            // 刷新页面
            this.getdata()
          })
      })
    },
    // 获取数据
    getdata() {
      this.customerData.page = this.current
      this.customerData.limit = this.limit
      customerApi.allCustomer(this.customerData).then(
        res => {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      )
    },
    // 分页获取
    getPagedata(page = 1) {
      this.current = page
      this.customerData.page = this.current
      this.customerData.limit = this.limit
      customerApi.allCustomer(this.customerData).then(
        res => {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      )
    },
    update(row) {
      this.updatedialogVisible = true
      this.customersInfo.id = row.id
      this.customersInfo.name = row.name
      this.customersInfo.address = row.address
      this.customersInfo.email = row.email
      this.customersInfo.phone = row.phone
    },
    updateCustomer(customersInfo) {
      this.$refs[customersInfo].validate((valid) => {
        if (valid) {
          // 添加商品
          console.log(this.customersInfo)
          customerApi.updateCustomer(this.customersInfo).then(
            res => {
              if (res.code === 200) {
                this.$message({
                  type: 'success',
                  message: '修改成功'
                })
                this.updatedialogVisible = false
                // 刷新页面
                this.getdata()
              }
            }
          )
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    addCustomer(customerInfo) {
      this.$refs[customerInfo].validate((valid) => {
        if (valid) {
          // 添加商品
          customerApi.addCustomer(this.customerInfo).then(
            res => {
              if (res.code === 500) {
                this.$message({
                  type: 'warning',
                  message: '该客户已存在!'
                })
                this.dialogVisible = false
                this.customerInfo = {}
              }
              if (res.code === 200) {
                this.$message({
                  type: 'success',
                  message: '添加成功'
                })
                this.dialogVisible = false
                this.customerInfo = {}
                // 刷新页面
                this.getdata()
              }
            }
          )
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.$message('取消操作')
          done()
        })
        .catch(_ => {})
    }
  }
}
</script>

<style scoped lang="scss">

</style>

